package org.potiorb.orb.marshal;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Iterator;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.Node;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;

import org.w3c.dom.Document;

public class MarshalerSOAP {

	public static final String[] LocalName = {"TimeRequest", "getTimeAsString", "getTimeAsElapsed"};
    public static final String Namespace = "http://exe/";
    public static final String NamespacePrefix = "ns2";

    public ByteArrayOutputStream out;
    public ByteArrayInputStream in;

    public void extract_contents_and_print(SOAPMessage msg) {
       try {
         SOAPBody body = msg.getSOAPBody();

         Name lookup_name = create_qname(msg,0);
         Iterator it = body.getChildElements(lookup_name);
         Node next = (Node) it.next();
    
         String value = (next == null) ? "Error!" : next.getValue();
         System.out.println("\n\nReturned from server: " + value);
       }
       catch(SOAPException e) { System.err.println(e); }
    }
    
    public SOAPMessage stringToSOAP(String msg){
    	SOAPMessage soap = null;
    	try {
    		soap = create_soap_message();
	    	SOAPPart soapPart = soap.getSOAPPart();
			
	    	// Load the SOAP text into a stream source  
	        byte[] buffer                 = msg.getBytes();  
	        ByteArrayInputStream stream   = new ByteArrayInputStream(buffer);  
	        
	        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
	        dbFactory.setNamespaceAware(true);
	        DocumentBuilder builder = dbFactory.newDocumentBuilder();
	        Document document = builder.parse(stream);
	        DOMSource domSource = new DOMSource(document);
        
			soapPart.setContent(domSource);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return soap;
    }
    
    public String extract_contents_string(SOAPMessage msg) {
    	String value = "";
    	try {
          SOAPPart p = msg.getSOAPPart();
          value = p.getFirstChild().getTextContent();
        }
        catch(Exception e) { 
        	
        }
		return value;
     }

    public SOAPMessage create_soap_message() {
       SOAPMessage msg = null;
       try {
         MessageFactory mf = MessageFactory.newInstance();
         msg = mf.createMessage();
       }
       catch(SOAPException e) { System.err.println(e); }
       return msg;
    }

    public SOAPMessage create_soap_message(InputStream in) {
       SOAPMessage msg = null;
       try {
         MessageFactory mf = MessageFactory.newInstance();
         msg = mf.createMessage(null, // ignore MIME headers
                                in);  // stream source
       }
       catch(SOAPException e) { System.err.println(e); }
       catch(IOException e) { System.err.println(e); }
       return msg;
    }
    
    public SOAPMessage create_soap_message(String in) throws SOAPException {
		SOAPMessage replyMsg = null;

		MessageFactory msgFactory = MessageFactory.newInstance();  
		replyMsg = msgFactory.createMessage();  
		SOAPPart soapPart = replyMsg.getSOAPPart();  
		// Load the SOAP text into a stream source  
		byte[] buffer = in.getBytes();  
		ByteArrayInputStream stream = new ByteArrayInputStream(buffer);  
		StreamSource source = new StreamSource(stream);
		// Set contents of message   
		soapPart.setContent(source);

		return replyMsg;
	}
    
    

    public Name create_qname(SOAPMessage msg, int method) {
       Name name = null;
       try {
         SOAPEnvelope env = msg.getSOAPPart().getEnvelope();
         name = env.createName(LocalName[method], 
         NamespacePrefix, 
         Namespace);
       }
       catch(SOAPException e) { System.err.println(e); }
       return name;
    }

    public void trace(String s, SOAPMessage m) {
       System.out.println("\n");
       System.out.println(s);
       try {
         m.writeTo(System.out);
       }
       catch(SOAPException e) { System.err.println(e); }
       catch(IOException e) { System.err.println(e); }
    }

}
